package info.cs52.fes.prolog;

import alice.tuprolog.Library;
import alice.tuprolog.Struct;
import alice.tuprolog.Term;
import alice.tuprolog.Var;

public class KnowledgeLibrary extends Library {

	private StringBuilder kb;

	public KnowledgeLibrary(String initialKnowledge) {
		kb = new StringBuilder(initialKnowledge);
	}
	
	public void addKnowledge(String s) {
		kb.append(s).append(".\n");
	}

	public boolean add_knowledge_1(Struct s) {
		kb.append(s).append(".\n");
		return true;
	}
	
	public String getKnowledge() {
		return kb.toString();
	}
	
	private String question = null;
	private String prompt;

	public boolean ask_2(Term attrib, Term promptTerm) {
		if (question == null) {
			question = attrib.toString();
			question = question.split("/")[1].trim();
			if (promptTerm instanceof Var) {
				promptTerm = ((Var) promptTerm).getTerm();
			}
			prompt = promptTerm.toString();
		}
		return true;
	}
	
	public String getQuestion() {
		return question;
	}
	
	public String getPrompt() {
		return prompt;
	}
	
	@Override
	public String getName() {
		return "user knowledge";
	}

}
